package com.dcits.electronic_account.service.impl;

import com.dcits.electronic_account.bean.BingBank;
import com.dcits.electronic_account.bean.Flow;
import com.dcits.electronic_account.mapper.BankAccountMapper;
import com.dcits.electronic_account.mapper.VirtualBanlanceMapper;
import com.dcits.electronic_account.mapper.WithdrawalMapper;
import com.dcits.electronic_account.service.WithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class WithdrawalImpl implements WithdrawalService {

    @Autowired
    public WithdrawalMapper withdrawalMapper;
    @Autowired
    public VirtualBanlanceMapper virtualBanlanceMapper;
    @Autowired
    private BankAccountMapper bankAccountMapper;

    @Override
    public int check_login_state(String id) {
        return withdrawalMapper.check_login_state(id);
    }

    @Override
    public List<BingBank> checkBingBankCard(String id) {
        return withdrawalMapper.checkBingBankCard(id);
    }

    @Override
    public String checkBankCardAndPassword(String bankCardNumber, String password) {
        return withdrawalMapper.checkBankCardAndPassword(bankCardNumber, password);
    }

    @Override
    @Transactional
    public int withdrawal(String bankCardNumber, BigDecimal balance,String id) {
        BigDecimal bigDecimal = virtualBanlanceMapper.virtualAccountBalance(Integer.parseInt(id));
        int flag = change_balance(bigDecimal.add(balance),bigDecimal.subtract(balance),bankCardNumber);
        if(flag>0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int check_balance(String bank_card_number, BigDecimal balance) {
        return withdrawalMapper.check_balance(bank_card_number,balance);
    }

    @Override
    public void insert_flow(Flow flow_account) {
        System.out.println(bankAccountMapper.selectIdByNumber(flow_account.getPayee()));
        flow_account.setPayee(String.valueOf(bankAccountMapper.selectIdByNumber(flow_account.getPayee())));
        withdrawalMapper.insert_flow(flow_account);
    }

    @Override
    public int select_fid() {
        return withdrawalMapper.select_fid();
    }

    @Override
    public int change_balance(BigDecimal add, BigDecimal reduce,String bank_card_number) {
        return withdrawalMapper.change_balance(add, reduce,bank_card_number);
    }
}
